//
//  CacheSizeModel.m
//  ITHome
//
//  Created by lanou3g on 15/7/20.
//  Copyright (c) 2015年 YAX. All rights reserved.
//

#import "CacheSizeModel.h"

@implementation CacheSizeModel

+ (CacheSizeModel *)shareInstance
{
    static CacheSizeModel *model = nil;
    static  dispatch_once_t onceToken;  // 锁
    dispatch_once ( &onceToken, ^ {     // 最多调用一次
        model = [[CacheSizeModel  alloc] init];
    });
    return  model;
}
//
//+ (float)fileSizeAtPath:(NSString *)path
//{
//    NSFileManager *fileManger = [NSFileManager defaultManager];
//    if ([fileManger fileExistsAtPath:path]) {
//        long long size = [fileManger attributesOfItemAtPath:path error:nil].fileSize;
//        return size/1024.0/1024.0;
//    }
//    return 0;
//}
//
//+ (float)folerSize
//{
//    NSString *path = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) objectAtIndex:0];
//    YAXLog(@"%@",path);
//    NSFileManager *fileManager = [NSFileManager defaultManager];
//    float folerSize;
//    if ([fileManager fileExistsAtPath:path]) {
//        NSArray *childerFiles = [fileManager subpathsAtPath:path];
//        for (NSString *fileName in childerFiles) {
//            NSString *absolutePath = [path stringByAppendingPathComponent:fileName];
//            folerSize += [self fileSizeAtPath:absolutePath];
//        }
//        folerSize += [[SDImageCache sharedImageCache] getSize]/1024.0/1024.0;
//
//        return folerSize;
//    }
//    return 0;
//}

- (float)checkTmpSize
{
    float totalSize = 0;
    NSString *path = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) objectAtIndex:0];
    YAXLog(@"%@",path);
    NSDirectoryEnumerator *fileEnumerator = [[NSFileManager defaultManager] enumeratorAtPath:path];
    YAXLog(@"%@",fileEnumerator);
    for (NSString *fileName in fileEnumerator)
    {
        NSString *filePath = [path stringByAppendingPathComponent:fileName];
        
        NSDictionary *attrs = [[NSFileManager defaultManager] attributesOfItemAtPath:filePath error:nil];
        
        unsigned long long length = [attrs fileSize];
        
        totalSize += length / 1000.0 / 1000.0;
    }
    //    NSLog(@"tmp size is %.2f",totalSize);
    
    return totalSize;
}
#pragma - mark - 清理Cacha文件中所有的文件
+ (void)clearCache
{
    NSString *path = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) objectAtIndex:0];
    NSFileManager *fileManager = [NSFileManager defaultManager];
    if ([fileManager fileExistsAtPath:path]) {
        NSArray *childerFiles = [fileManager subpathsAtPath:path];
        for (NSString *fileName in childerFiles) {
            //如果有需要，添加条件，过滤掉不想清理的文件
            NSString *absolutePath = [path stringByAppendingPathComponent:fileName];
            [fileManager removeItemAtPath:absolutePath error:nil];
        }
    }
    [[SDImageCache sharedImageCache] cleanDisk];
}

#pragma 只清理缓存图片

- (void)clearTmpPics
{
    [[SDImageCache sharedImageCache] clearDisk];
    
    [[SDImageCache sharedImageCache] clearMemory];//可有可无
    
    YAXLog(@"clear disk");
    
    //未用到(如果缓存中的大小大于1M,以M的形式显示提示.否者以K)
    float tmpSize = [self checkTmpSize];
    YAXLog(@"++++++%f",tmpSize);
    NSString *clearCacheName = tmpSize >= 1 ? [NSString stringWithFormat:@"清理缓存(%.2fM)",tmpSize] : [NSString stringWithFormat:@"清理缓存(%.2fK)",tmpSize * 1024];
    YAXLog(@"/////%@",clearCacheName);
//    [configDataArray replaceObjectAtIndex:2 withObject:clearCacheName];
    
//    [configTableView reloadData];
}
@end
